package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.param.AddRealidParam;
import com.jiangxiaofei.citycloud.param.PhoneCodeParam;
import com.jiangxiaofei.citycloud.param.UpLoadPhotoParam;
import com.jiangxiaofei.citycloud.param.UserInfoParam;
import com.jiangxiaofei.citycloud.param.UserRegistParam;
import com.jiangxiaofei.citycloud.param.UserStateParam;
import com.jiangxiaofei.citycloud.util.T;

import java.util.List;

/**
 * Created by JiangXFei on 2017/8/23.
 */

public class MainPersenter implements MainConstant.UploadPhotoPersenter,
        MainConstant.AdduserRealInfoPersenter, MainConstant.GetRealUserInfoPersenter,
        MainConstant.UserRegistPersenter, MainConstant.PhoneIsRepeatPersenter,
        MainConstant.UserLoginPersenter, MainConstant.GetBannerPersenter,
        MainConstant.GetPhoneCodePersenter, MainConstant.UpdatePhonePersenter,
        MainConstant.UpdatePwdPersenter, MainConstant.UpdateUserNamePersenter,
        MainConstant.UserRepeatNamePersenter, MainConstant.FindUserPwdPersenter {
    Context mContext;
    //上传图片
    MainConstant.UpLoadPhotoModel mUpLoadPhotoModel;
    MainConstant.UpLoadPhotoView mUpLoadPhotoView;

    MainConstant.AddUserRealInfoModel addUserRealInfoModel;
    MainConstant.AddUserRealInfoView addUserRealInfoView;

    MainConstant.GetRealUserInfoModel getRealUserInfoModel;
    MainConstant.GetRealUserInfoView getRealUserInfoView;

    MainConstant.UserRegistModel userRegistModel;
    MainConstant.UserRegistView userRegistView;

    MainConstant.PhoneIsRepeatModel phoneIsRepeatModel;
    MainConstant.PhoneIsRepeatView phoneIsRepeatView;

    MainConstant.UserLoginModel userLoginModel;
    MainConstant.UserLoginView userLoginView;

    MainConstant.GetBannerModel getBannerModel;
    MainConstant.GetBannerView getBannerView;

    MainConstant.GetPhoneCodeModel getPhoneCodeModel;
    MainConstant.GetPhoneCodeView getPhoneCodeView;

    MainConstant.UpdatePhoneModel updatePhoneModel;
    MainConstant.UpdatePhoneView updatePhoneView;

    MainConstant.UpdateUserNameModel updateUserNameModel;
    MainConstant.UpdateUserNameView updateUserNameView;

    MainConstant.UpdatePwdModel updatePwdModel;
    MainConstant.UpdatePwdView updatePwdView;

    MainConstant.UserRepeatNameModel userRepeatNameModel;
    MainConstant.UserRepeatNameView userRepeatNameView;

    MainConstant.FindUserPwdModel findUserPwdModel;
    MainConstant.FindUserPwdView findUserPwdView;

    public MainPersenter(Context mContext, MainConstant.UpdatePhoneView updatePhoneView) {
        this.mContext = mContext;
        this.updatePhoneView = updatePhoneView;
        this.updatePhoneModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.UpdateUserNameView updateUserNameView) {
        this.mContext = mContext;
        this.updateUserNameView = updateUserNameView;
        this.updateUserNameModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.UpdatePwdView updatePwdView) {
        this.mContext = mContext;
        this.updatePwdView = updatePwdView;
        this.updatePwdModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.UserRepeatNameView userRepeatNameView) {
        this.mContext = mContext;
        this.userRepeatNameView = userRepeatNameView;
        this.userRegistModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.FindUserPwdView findUserPwdView) {
        this.mContext = mContext;
        this.findUserPwdView = findUserPwdView;
        this.findUserPwdModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.GetPhoneCodeView getPhoneCodeView) {
        this.mContext = mContext;
        this.getPhoneCodeView = getPhoneCodeView;
        this.getPhoneCodeModel = new MainModel();
    }


    public MainPersenter(Context mContext, MainConstant.UserRegistView userRegistView) {
        this.mContext = mContext;
        this.userRegistView = userRegistView;
        this.userRegistModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.PhoneIsRepeatView phoneIsRepeatView) {
        this.mContext = mContext;
        this.phoneIsRepeatView = phoneIsRepeatView;
        this.phoneIsRepeatModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.UpLoadPhotoView mUpLoadPhotoView) {
        this.mContext = mContext;
        this.mUpLoadPhotoView = mUpLoadPhotoView;
        mUpLoadPhotoModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.AddUserRealInfoView addUserRealInfoView) {
        this.mContext = mContext;
        this.addUserRealInfoView = addUserRealInfoView;
        addUserRealInfoModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.GetRealUserInfoView getRealUserInfoView) {
        this.mContext = mContext;
        this.getRealUserInfoView = getRealUserInfoView;
        getRealUserInfoModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.UserLoginView userLoginView) {
        this.mContext = mContext;
        this.userLoginView = userLoginView;
        this.userLoginModel = new MainModel();
    }

    public MainPersenter(Context mContext, MainConstant.GetBannerView getBannerView) {
        this.mContext = mContext;
        this.getBannerView = getBannerView;
        this.getBannerModel = new MainModel();
    }

    /**
     * 上传图片
     */
    @Override
    public void uploadData() {
        UpLoadPhotoParam upLoadPhotoParam = mUpLoadPhotoView.uploadPhoto();
        if (upLoadPhotoParam != null) {
            mUpLoadPhotoModel.uploadPhoto(mContext, upLoadPhotoParam.getPhotoFile(), new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (o == null) {
                        T.showShort(mContext, "网络错误");
                    } else {
                        mUpLoadPhotoView.uploadData((String) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    T.showShort(mContext, msg);
                }
            });
        }
    }

    //添加用户实名信息
    @Override
    public void addUserRealInfo() {
        if (EmptyUtils.isNotEmpty(addUserRealInfoView.addUserRealInfo())) {
            AddRealidParam addRealidParam = addUserRealInfoView.addUserRealInfo();
            addUserRealInfoModel.addUserRealInfo(mContext, addRealidParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    addUserRealInfoView.setData(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    //获取用户实名信息
    @Override
    public void getRealUserInfo() {
        if (getRealUserInfoView.getRealUserInfo() > 0) {
            getRealUserInfoModel.getRealUserInfo(mContext, getRealUserInfoView.getRealUserInfo(), new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        getRealUserInfoView.setUserRealInfo(o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 用户注册
     */
    @Override
    public void onRegistUser() {
        UserRegistParam param = userRegistView.getRegistParam();
        if (EmptyUtils.isNotEmpty(param)) {
            userRegistModel.registUser(mContext, param, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    userRegistView.onRegistSuccess(o);
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 验证用户手机号
     */
    @Override
    public void repeatIsPhone() {
        String phoneNum = phoneIsRepeatView.getPhoneNum();
        if (EmptyUtils.isNotEmpty(phoneNum)) {
            phoneIsRepeatModel.phoneRepeat(mContext, phoneNum, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (resultCode == 1) {
                        phoneIsRepeatView.repeatPhone(o);
                    } else {
                        Toast.makeText(mContext, "手机号已被注册！", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 用户登陆
     */
    @Override
    public void onLoginUser() {
        UserInfoParam userLoginParam = userLoginView.getUserLoginParam();
        if (EmptyUtils.isNotEmpty(userLoginParam)) {
            userLoginModel.userLogin(mContext, userLoginParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        userLoginView.onLoginSuccess(o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 获取banner
     */
    @Override
    public void getBanner() {
        int id = getBannerView.getBannerParam();
        if (id >= 0) {
            getBannerModel.getBanner(mContext, id, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        getBannerView.bannerSuccess((List<BannerBean>) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    @Override
    public void getPhoneCode() {
        PhoneCodeParam param = getPhoneCodeView.getCodePhoneNum();
        if (EmptyUtils.isNotEmpty(param)) {
            getPhoneCodeModel.getPhoneCode(mContext, param, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    getPhoneCodeView.getPhoneCodeMsg(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    /**
     * 修改用户密码persenter
     */
    @Override
    public void updatePwd() {
        UserStateParam updatePwdParam = updatePwdView.getUpdatePwdParam();
        if (EmptyUtils.isNotEmpty(updatePwdParam)) {
            updatePwdModel.updatePwd(mContext, updatePwdParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    updatePwdView.updatePwdSuccess(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    /**
     * 修改手机号码
     */
    @Override
    public void updatePhone() {
        UserStateParam updatePhoneParam = updatePhoneView.getUpdatePhoneParam();
        if (EmptyUtils.isNotEmpty(updatePhoneParam)) {
            updatePhoneModel.updatePhone(mContext, updatePhoneParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    updatePhoneView.updatePhoneSuccess(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }

    }

    /**
     * 求改用户名
     */
    @Override
    public void updateUserName() {
        UserStateParam updateUserNameParam = updateUserNameView.getUpdateUserNameParam();
        if (EmptyUtils.isNotEmpty(updateUserNameParam)) {
            updateUserNameModel.updateUserNmae(mContext, updateUserNameParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    updateUserNameView.updateUserNameSuccess(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    /**
     * 检查用户名是否重复
     */
    @Override
    public void repeatName() {
        String userName = userRepeatNameView.getUserName();
        if (EmptyUtils.isNotEmpty(userName)) {
            userRepeatNameModel.RepeatName(mContext, userName, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    userRepeatNameView.repeatName(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast("用户名已存在！");
                }
            });
        }
    }

    /**
     * 找回密码
     */
    @Override
    public void findPwd() {
        UserStateParam pwdParam = findUserPwdView.findPwdParam();
        if (EmptyUtils.isNotEmpty(pwdParam)) {
            findUserPwdModel.findPwd(mContext, pwdParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    findUserPwdView.findPwdSuccess(message);
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    private void toast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
}
